﻿#include "pipelinemanager.h"
#include <QPluginLoader>
#include <QCoreApplication>
#include <QDir>
#include "pipeline_log.h"
#include "pipelinepluginbase.h"
// 临时

PipelineManager::PipelineManager()
{
}

PipelineManager::~PipelineManager()
{
}

static PipelineManager* instance = nullptr;
PipelineManager* PipelineManager::GetInstance()
{
	if (instance == nullptr) {
		instance = new PipelineManager();
	}
	return instance;
}

bool PipelineManager::LoadProjectPlugins(const QString& proname)
{
	log_trace("load project grabPos");
	QString dir = QCoreApplication::applicationDirPath();
	dir = QDir(dir).absolutePath();

	if (!dir.endsWith('/') && !dir.endsWith('\\')) {
		dir.append('/');
	}

	dir.append("project/");
	QDir d(dir);
	QStringList pl = d.entryList({ "*.dll" }, QDir::Files);
	for (auto f : pl) {
		QPluginLoader loader(dir + f);
		if (loader.load()) {
			PipelinePluginBase* tmp = qobject_cast<PipelinePluginBase*>(loader.instance());
			if (tmp != nullptr) {
				QJsonObject o = loader.metaData();
				if (o.contains("IID")) {
					QString iid = o["IID"].toString();
					log_trace("IID = {}", iid);
					QString name = iid.split('.').last();
					if (name == proname) {
						m_pro = tmp;
						break;
					}
				}
			}
			loader.unload();
		}
	}
	return m_pro != nullptr;
}

Pipeline* PipelineManager::CreatePipeline(PipelineType type)
{
	return m_pro->CreatePipeline(type);
}
